package com.wolf.spring;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @auth: dylan
 * @Desc:
 * @Time: 2024/5/2
 */
@Component
public class WolfContextUtil implements ApplicationContextAware {
    private volatile  static ApplicationContext context;
    @Override
    public void setApplicationContext(ApplicationContext context) throws BeansException {
        if(WolfContextUtil.context==null){
            synchronized (WolfContextUtil.class){
                if(WolfContextUtil.context==null){
                    WolfContextUtil.context=context;
                }
            }
        }
    }

    public static <T> T getBean(Class<T> clazz){
        return context.getBean(clazz);
    }
}
